package cn.com.nwpu.visual_system.service.impl;

import cn.com.nwpu.visual_system.entity.*;
import cn.com.nwpu.visual_system.mapper.*;
import cn.com.nwpu.visual_system.service.ReactorService;
import cn.com.nwpu.visual_system.vo.ReactorDetailVo;
import cn.com.nwpu.visual_system.vo.ReactorVo;
import cn.com.nwpu.visual_system.vo.ToVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Hutengzhong
 * @version 1.0.0
 * @title ReactorServiceImpl
 * @description TODO
 * @date 2024/4/27 16:45
 */
@Service
public class ReactorServiceImpl implements ReactorService {
    @Autowired
    ReactorMapper reactorMapper;
    @Autowired
    SensorMapper sensorMapper;
    @Autowired
    SensorProcessDataMapper sensorProcessDataMapper;
    @Autowired
    ConfigDataMapper configDataMapper;

    @Override
    public List<ReactorVo> getReactorList() {
        List<ReactorVo> list = ToVo.reactorListToVO(reactorMapper.getReactorList());
        return list;
    }

    @Override
    public List<ReactorDetailVo> getReactorSensorDetailList(Integer id) {
        // 获取传感器所属的反应器id
        Reactor reactor = reactorMapper.getReactorById(id);
        // 获取过程变量的传感器信息
        List<Sensor> sensorProcessList = sensorMapper.getSensorProcessListByReactorId(id);
        Integer reactorId = reactor.getReactorId();
        // 获取当前数据的最新值
        ConfigData configData = configDataMapper.getCurrentTestDataId();
        Integer currentDataId = Integer.parseInt(configData.getConfigValue());
        List<SensorProcessData> sensorProcessDataList = new ArrayList<>();
        switch (reactorId) {
            // 反应器
            case 3:
                sensorProcessDataList = sensorProcessDataMapper.getReactorSensorDataList(currentDataId);
                break;
            // 气液分离器
            case 4:
                sensorProcessDataList = sensorProcessDataMapper.getSeparatorSensorDataList(currentDataId);
                break;
            // 汽提塔
            case 5:
                sensorProcessDataList = sensorProcessDataMapper.getStripperSensorDataList(currentDataId);
                break;
            // 压缩机
            case 6:
                sensorProcessDataList = sensorProcessDataMapper.getCompressorSensorDataList(currentDataId);
                break;
            // 冷凝器
            case 7:
                sensorProcessDataList = sensorProcessDataMapper.getCondenserSensorDataList(currentDataId);
                break;
        }

        // 每次访问后，自动加一，模拟实时数据（用id的递增代替时间的增加）
        // 交由最后访问的测试函数来更新
//        Integer count = configDataMapper.updateTestDataId(currentDataId + 1);

        List<ReactorDetailVo> reactorDetailVoList = ToVo.toReactorDetailVoList(sensorProcessList, sensorProcessDataList);
        return reactorDetailVoList;
    }
}
